package com.example.home;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.home.fragment.HelpFragment;
import com.example.home.fragment.HomeFragment;
import com.example.home.fragment.MeFragment;
import com.example.home.fragment.NewsFragment;
import com.example.home.fragment.ServlerFragment;
import com.example.home.me.LoginActivity;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView botview;
    private HelpFragment helpFragment;
    private HomeFragment homeFragment;
    private ServlerFragment servlerFragment;
    private MeFragment meFragment;
    private NewsFragment newsFragment;
    private BotviewReceiven botviewReceiven;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        botview = findViewById(R.id.botview);
        helpFragment = new HelpFragment();
        homeFragment = new HomeFragment();
        newsFragment = new NewsFragment();
        meFragment = new MeFragment();
        servlerFragment = new ServlerFragment();

        botviewReceiven = new BotviewReceiven();
        IntentFilter filter = new IntentFilter();
        filter.addAction("bov_action1");
        filter.addAction("bov_action2");
        registerReceiver(botviewReceiven,filter);

        isToken();


        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian,homeFragment).commit();
        botview.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, homeFragment).commit();
                        break;
                    case R.id.help:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, helpFragment).commit();
                        break;
                    case R.id.servler:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, servlerFragment).commit();
                        break;
                    case R.id.news:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, newsFragment).commit();
                        break;
                    case R.id.me:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, meFragment).commit();
                        break;
                }
                return true;
            }
        });




    }

    public class BotviewReceiven extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("bov_action1".equals(action)){
                botview.setSelectedItemId(R.id.help);
            }else if("bov_action2".equals(action)){

            }
        }
    }

    void isToken() {
        // 获取 SharedPreferences 对象
        SharedPreferences sharedPreferences = getSharedPreferences("user_data", Context.MODE_PRIVATE);
        // 从 SharedPreferences 中检索请求头/令牌
        String authToken = sharedPreferences.getString("auth_token", null);
        OKHttpTool.TOKEN = authToken;
        if (TextUtils.isEmpty(authToken)) {
            //跳转
            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }else{
            String isToMeFragment = getIntent().getStringExtra("isToMeFragment");
            if ("y".equals(isToMeFragment)) {
                getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, homeFragment).commit();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(botviewReceiven);
    }
}